package com.fzfnn.scrapbook.mapper;

import com.fzfnn.scrapbook.dto.JournalDto.JournalDto;
import com.fzfnn.scrapbook.entity.CollectionFolder;
import com.fzfnn.scrapbook.vo.CollectionCountVO;
import com.fzfnn.scrapbook.vo.JournalVo;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;

@Mapper
public interface CollectionMapper {

    // 创建收藏夹
    @Insert("INSERT INTO collection_folder (user_id, name) VALUES (#{userId}, #{name})")
    int createFolder(@Param("userId") Long userId, @Param("name") String name);
    //返回收藏夹id
    @Select("SELECT id FROM collection_folder WHERE user_id = #{userId} AND name = #{name}")
    Long getFolderIdByName(@Param("userId") Long userId, @Param("name") String name);

    // 获取用户收藏夹列表
    @Select("SELECT id, user_id AS userId,name,created_at AS createdAt FROM collection_folder WHERE user_id = #{userId}")
    List<CollectionFolder> getFoldersByUserId(@Param("userId") Long userId);
    // 获取收藏夹下的收藏数量
    @Select({
            "<script>",
            "SELECT folder_id AS folderId, COUNT(*) AS count",
            "FROM journal_collection",
            "WHERE folder_id IN",
            "<foreach item='id' collection='folderIds' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "GROUP BY folder_id",
            "</script>"
    })

    List<CollectionCountVO> getCollectionCountsByFolderIds(@Param("folderIds") List<Long> folderIds);

    // 添加收藏
    @Insert("INSERT INTO journal_collection (user_id, journal_id, folder_id) VALUES (#{userId}, #{journalId}, #{folderId})")
    int addCollection(@Param("userId") Long userId, @Param("journalId") Long journalId, @Param("folderId") Long folderId);

    // 判断是否已收藏
    @Select("SELECT COUNT(*) FROM journal_collection WHERE user_id = #{userId} AND journal_id = #{journalId}")
    boolean isCollected(@Param("userId") Long userId, @Param("journalId") Long journalId);

    // 取消收藏
    @Delete("DELETE FROM journal_collection WHERE user_id = #{userId} AND journal_id = #{journalId}")
    int removeCollection(@Param("userId") Long userId, @Param("journalId") Long journalId);

    // 根据收藏夹ID删除该收藏夹下的所有收藏
    @Delete("DELETE FROM journal_collection WHERE folder_id = #{folderId} and user_id = #{userId}")
    void deleteCollectionsByFolderId(@Param("folderId")Long folderId, @Param("userId") Long userId);

    @Delete("DELETE FROM collection_folder WHERE id = #{folderId} and user_id = #{userId}")
    void deleteFolderById(@Param("folderId")Long folderId,@Param("userId") Long userId);

    //  获取收藏夹下的所有收藏
    @Select("SELECT j.journal_id AS journalId, j.content, j.like_count AS likeCount, " +
            "j.title, j.user_id AS userId, j.updated_at AS updatedAt, j.created_at AS createdAt, " +
            "j.img_url AS imgurl, j.tag, c.collected_at AS createdAty, " +
            "u.username AS username, u.imgurl AS avatarUrl " +
            "FROM journal j " +
            "JOIN journal_collection c ON j.journal_id = c.journal_id " +
            "JOIN user u ON j.user_id = u.user_id " +
            "WHERE c.folder_id = #{folderId} AND c.user_id = #{userId}")
    List<JournalVo> getCollectionsByFolderId(@Param("userId") Long userId, @Param("folderId") Long folderId);


    //修改收藏夹名称
    @Update("UPDATE collection_folder SET name = #{name} WHERE id = #{folderId} AND user_id = #{userId}")
    int updateFolderName(@Param("userId") Long userId, @Param("folderId") Long folderId, @Param("name") String name);
    //查询收藏夹名称
    @Select("SELECT name FROM collection_folder WHERE id = #{folderId} AND user_id = #{userId}")
    String getFolderNameById(@Param("userId") Long userId, @Param("folderId") Long folderId);


}
